import {Loading, Message, Notification} from 'element-ui'

/**
 * 是否为空
 *
 * @param {any} str 字符串
 * @return {boolean}
 */
export const isEmpty = (str: any): boolean => {
  switch (typeof str) {
    case 'undefined':
      return true
    case 'string':
      if (str.replace(/(^[ \t\n\r]*)|([ \t\n\r]*$)/g, '').length === 0) return true
      break
    case 'boolean':
      if (!str) return true
      break
    case 'number':
      if (str === 0 || isNaN(str)) return true
      break
    case 'object':
      if (str == null || str.length === 0) return true
      for (const i in str) {
        return false
      }
      return true
  }
  return false
}

/**
 * 是否为数字
 *
 * @param {int} int 整数
 *
 * @return boolean
 */
export const isNumber = (int: number): boolean => {
  return int === +int
}

/**
 * 是否为object
 *
 * @param {object} obj 对象
 * @return {boolean}
 */
export const isObject = (obj: any): boolean => {
  // return Object.prototype.toString.call(obj) === '[Object Object]'
  return typeof obj === 'object'
}

/**
 * 是否为数组
 * @param arg
 * @return {boolean}
 */
export const isArray = (arg: any): boolean => {
  return Object.prototype.toString.call(arg) === '[object Array]'
}

/**
 * 验证手机号码
 *
 * @param value
 * @return {boolean}
 */
export const isMobile = (value: string): boolean => {
  if (isEmpty(value)) {
    return false
  }
  return value.length === 11 && /^1[3-9]\d{9}$/.test(value)
}

/**
 * 验证邮箱
 *
 * @param value
 * @return {boolean}
 */
export const isEmail = (value: string): boolean => {
  const emailTest = /^([a-zA-Z0-9]+[_|.]?)*[a-zA-Z0-9]+@([a-zA-Z0-9]+[_|.]?)*[a-zA-Z0-9]+\.[a-zA-Z]{2,3}$/
  return emailTest.test(value)
}

/**
 * 验证用户名
 *
 * @param value
 * @return {boolean}
 */
export const isUsername = (value: string): boolean => {
  return /^[a-zA-Z][a-zA-Z0-9_]{4,14}$/.test(value)
}

/**
 * 判断目标是否是函数
 *
 * @param fun
 * @return {boolean}
 */
export const isFunction = (fun: any): boolean => {
  return typeof fun === 'function'
}

/**
 * 时间格式化
 *
 * @param fmt
 * @param time
 * @return {string}
 */
export const dateFormat = (fmt: string, time: any): string => {
  const _time = new Date(time)

  const o: any = {
    'M+': _time.getMonth() + 1,
    'd+': _time.getDate(),
    'h+': _time.getHours(),
    'm+': _time.getMinutes(),
    's+': _time.getSeconds(),
    'q+': Math.floor((_time.getMonth() + 3) / 3),
    'S': _time.getMilliseconds()
  }

  if (/(y+)/.test(fmt)) {
    fmt = fmt.replace(RegExp.$1, (_time.getFullYear() + '').substr(4 - RegExp.$1.length))
  }

  for (const k in o) {
    if (new RegExp('(' + k + ')').test(fmt)) {
      fmt = fmt.replace(RegExp.$1, (RegExp.$1.length === 1) ? (o[k]) : (('00' + o[k]).substr(('' + o[k]).length)))
    }
  }

  return fmt
}

/**
 * 转JSON
 *
 * @param json
 * @returns {any}
 */
export const toJson = (json: any): any => {
  return JSON.parse(JSON.stringify(json))
}

/**
 * localstorage 保存
 *
 * @param key
 * @param value
 * @return {boolean}
 */
export const setStorage = (key: string, value: string): boolean => {
  try {
    if (!window.localStorage) {
      errorMessage('浏览器不支持 localstorage')
      return false
    }
    localStorage.setItem(key, value)
    return true
  } catch (e) {
    return false
  }
}

/**
 * localStorage 读取
 *
 * @param key
 * @return {any}
 */
export const getStorage = (key: string): any => {
  return localStorage.getItem(key)
}

/**
 * localStorage 删除
 *
 * @param key
 * @return {boolean}
 */
export const removeStorage = (key: string): boolean => {
  try {
    localStorage.removeItem(key)
    return true
  } catch (e) {
    return false
  }
}

/**
 * localStorage 清空
 *
 * @return {boolean}
 */
export const clearStorage = (): boolean => {
  try {
    localStorage.clear()
    return true
  } catch (e) {
    return false
  }
}

/**
 * 加载中
 *
 * @param {Object} [options] 选项
 * @return {any}
 */
export const loading = (options?: any): any => {
  options = {
    ...{
      lock: true,
      text: 'Loading...',
      background: 'rgba(0, 0, 0, .3)'
    },
    ...options
  }
  
  return Loading.service(options)
}

/**
 * 消息提示
 *
 * @param options
 */
export const message = (options: any): any => {
  const opt = {
    message: '未知消息',
    duration: 2000
  }

  const option = {
    ...opt,
    ...options
  }

  return Message(option);
}

/**
 * 成功消息
 *
 * @param message
 */
export const successMessage = (message: string): any => {
  return Message({
    type: 'success',
    message: message || '操作成功',
    duration: 2500
  })
}

/**
 * 警告消息
 *
 * @param message
 */
export const warningMessage = (message: string): any => {
  Message({
    type: 'warning',
    message: message || '请求失败，或重试',
    duration: 2500
  })
}

/**
 * 错误消息
 *
 * @param message
 */
export const errorMessage = (message: string): any => {
  Message({
    type: 'error',
    message: message || '请求失败，或重试',
    duration: 2500
  })
}

/**
 * 通知
 *
 * @param options
 * @return {*}
 */
export const notification = (options: any): void => {
  Notification({
    title: '未知',
    message: '未知通知',
    duration: 1000,
    ...options
  })
}

/**
 * 错误通知
 *
 * @param message
 */
export const errorNotificationMessage = (message: string): void => {
  Notification({
    type: 'error',
    title: '操作失败',
    message: message || '请求失败，或重试',
    duration: 3000
  })
}

/**
 * 成功通知
 *
 * @param message
 */
export const successNotificationMessage = (message: string): void => {
  Notification({
    type: 'success',
    title: '操作成功',
    message: message || '请求失败，或重试',
    duration: 3000
  })
}

/**
 * 查找数组索引位置
 *
 * @param arr
 * @param attr
 * @param value
 * @return {*}
 */
export const arrayFindIndex = (arr: any, attr: string, value: any): number => {
  return arr.findIndex((item: any) => item[attr] === value)
}
